/**
 * Copyright (c) 2018-2099, Chill Zhuang 庄骞 (bladejava@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.restaurant.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.restaurant.entity.BusinessObjectives;
import org.springblade.modules.restaurant.mapper.BusinessObjectivesMapper;
import org.springblade.modules.restaurant.service.*;
import org.springblade.modules.restaurant.vo.BusinessObjectivesVO;
import org.springblade.modules.system.service.ITenantService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 营业目标表 服务实现类
 *
 * @author Blade
 * @since 2025-02-18
 */
@Slf4j
@Service
@AllArgsConstructor
public class BusinessObjectivesServiceImpl extends BaseServiceImpl<BusinessObjectivesMapper, BusinessObjectives> implements IBusinessObjectivesService {
	private IUserSalaryDayService userSalaryDayService;
	private ICommoditySummaryService commoditySummaryService;
	private IOperatingIncomeService operatingIncomeService;
	private ITenantService tenantService;
	private IExpensesCostService expensesCostService;
	private IExpensesBudgetService expensesBudgetService;

	@Override
	public IPage<BusinessObjectivesVO> selectBusinessObjectivesPage(IPage<BusinessObjectivesVO> page, BusinessObjectivesVO businessObjectives) {
		return page.setRecords(baseMapper.selectBusinessObjectivesPage(page, businessObjectives));
	}

	@Override
	public void updateBusinessObjectives(String time) {
		List<String> tenantIds = tenantService.getAllTenantIds();
		for (String tenantId : tenantIds) {

			String businessTime = DateUtil.format(DateUtil.parse(time), "yyyy-MM");
			BusinessObjectives businessObjectives = getOne(Wrappers.<BusinessObjectives>lambdaQuery()
				.eq(BusinessObjectives::getTenantId, tenantId)
				.eq(BusinessObjectives::getBusinessTime, businessTime));
			if (businessObjectives == null) {
				businessObjectives = new BusinessObjectives();
				businessObjectives.setBusinessTime(businessTime);
				businessObjectives.setTenantId(tenantId);
			}
			calculate(businessObjectives, time, tenantId);
		}
	}

	@Override
	public boolean calculate(BusinessObjectives businessObjectives, String time, String tenantId) {
		// 计算当月数据时： 总支出 = 薪资支出 + 预算支出 + 费用支出 + 采购支出
		// 计算非当月数据时： 总支出 = 薪资支出 + 费用支出 + 采购支出

		// 获取人员工资
		BigDecimal userSalarySum = userSalaryDayService.getUserDailySalaryMonthSum(time, tenantId);
		businessObjectives.setSalaryExpenses(userSalarySum);

		// 判断是否为当月
		boolean isSameMonth = DateUtil.isSameMonth(DateUtil.parse(time), DateUtil.date());
		// 预算日平均费用
		BigDecimal expensesBudgetSum = BigDecimal.ZERO;
		// 是否为当月，当月则计算已过去多少天 ，日平均 * 天数
		if (isSameMonth) {
			int day = DateUtil.dayOfMonth(DateUtil.parse(time));
			// 预算支出
			expensesBudgetSum = expensesBudgetService.getExpensesBudgetDayAvg(time, tenantId).multiply(new BigDecimal(day));
		}
		businessObjectives.setFixedExpenses(expensesBudgetSum);

		// 费用支出
		BigDecimal expensesCostSum = expensesCostService.getExpensesCostMonthSum(time, tenantId);
		businessObjectives.setOtherExpenses(expensesCostSum);

		// 采购支出
		BigDecimal commoditySummarySum = commoditySummaryService.getCommoditySummaryMonthSum(time, tenantId);
		businessObjectives.setProcurementExpenses(commoditySummarySum);

		// 总支出
		BigDecimal expendAmount = userSalarySum.add(expensesBudgetSum).add(commoditySummarySum).add(expensesCostSum);
		businessObjectives.setExpendAmount(expendAmount);

		// 经营收入
		BigDecimal incomeAmount = operatingIncomeService.getOperatingIncomeMonthSum(time, tenantId);
		businessObjectives.setIncomeAmount(incomeAmount);

		// 营业毛利 = 营业收入 - 采购支出 - 费用支出
		BigDecimal profitAmount = incomeAmount.subtract(commoditySummarySum).subtract(expensesCostSum);
		businessObjectives.setProfitAmount(profitAmount);

		// 营业净利 = 营业收入 - 采购支出 - 费用支出 - 预算支出 - 薪资支出 = 营业收入 - 总支出
		businessObjectives.setNetProfit(incomeAmount.subtract(expendAmount));
		return saveOrUpdate(businessObjectives);
	}
}
